package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Component
@Slf4j
public class SpringRabbitListener {
    //从simple.queue 队列中接受消息
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        log.info("SpringRabbitListener 消费者收到消息：{}", msg);
        System.out.println("从simple.queue队列中接收到消息:" + msg);
    }

    //消费者1：监听work.queue队列：每次处理完一个消息要沉睡20ms
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) {
        log.info("消费者1：监听work.queue队列：{}", msg);
        System.out.println("消费者1：从work.queue队列中接收到消息:" + msg);
        try {
            Thread.sleep(20);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //消费者2：监听work.queue队列：每次处理完一个消息要沉睡200ms
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) {
        log.info("消费者2：监听work.queue队列：{}", msg);
        System.out.println("消费者2：从work.queue队列中接收到消息:" + msg);
        try {
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //监听fanout.queue1队列的消息
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        log.info("消费者1：监听fanout.queue1队列：{}", msg);
        System.out.println("消费者1：从fanout.queue1队列中接收到消息:" + msg);
    }

    //监听fanout.queue2队列的消息
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        log.info("消费者2：监听fanout.queue2队列：{}", msg);
        System.out.println("消费者2：从fanout.queue1队列中接收到消息:" + msg);
    }
/*    //监听direct.queue1队列的消息
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg){
        log.info("消费者1：监听direct.queue1队列：{}",msg);
        System.out.println("消费者1：从direct.queue1队列中接收到消息:"+msg);
    }
    //监听direct.queue2队列的消息
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String message){
        log.info("消费者2：监听direct.queue2队列：{}",message);
        System.out.println("消费者2：从direct.queue2队列中接收到消息:"+message);
    }*/

    /*
        声明交换机与队列和其绑定
        消费者1：监听direct.queue1 ; 绑定hmall.direct ; routingKey为blue/red
     */
    @RabbitListener(bindings = @QueueBinding(
            //声明队列：durable = true 表示是持久化的队列
            value = @Queue(value = "direct.queue1", durable = "true"),
            //声明交换机
            exchange = @Exchange(value = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"blue", "red"}
    )
    )
    public void listenDirectQueue1(String msg) {
        log.info("消费者1：监听direct.queue1队列：{}", msg);
        System.out.println("消费者1：从direct.queue1队列中接收到消息:" + msg);
    }

    /*
        声明交换机与队列和其绑定
        消费者2：监听direct.queue2 ; 绑定hmall.direct ; routingKey为yellow/red
     */
    @RabbitListener(bindings = @QueueBinding(
            //声明队列：durable = true 表示是持久化的队列
            value = @Queue(value = "direct.queue2", durable = "true"),
            //声明交换机
            exchange = @Exchange(value = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"yellow", "red"}
    )
    )
    public void listenDirectQueue2(String message) {
        log.info("消费者2：监听direct.queue2队列：{}", message);
        System.out.println("消费者2：从direct.queue2队列中接收到消息:" + message);
    }

    //监听topic.queue1队列的消息
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message) {
        log.info("消费者1：监听topic.queue1队列：{}", message);
        System.out.println("消费者1：从topic.queue1队列中接收到消息:" + message);
    }

    //监听topic.queue2队列的消息
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message) {
        log.info("消费者2：监听topic.queue2队列：{}", message);
        System.out.println("消费者2：从topic.queue2队列中接收到消息:" + message);
    }

    //接受object.queue队列的消息；如果队列不存在则创建
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Object msg) {
        log.info("消费者1：监听object.queue队列：{}", msg);
        System.out.println("消费者1：从object.queue队列中接收到消息:" + msg);
    }

    //接收simple.queue2 队列的消息
            //none -> 抛出MessageConversionExceptionException消息转换异常，将被reject
            //auto -> 抛出业务异常；消息将被保留在队列
//    @RabbitListener(queues = "simple.queue2")
    public void listenSimpleQueue2(String msg) {
        log.info("消费者1：监听simple.queue2队列：{}", msg);
        System.out.println("消费者1：从simple.queue2队列中接收到消息:" + msg);
        //故意抛出消息格式转换异常；这时候会被reject；消息会被删除（配置中消息确认模式为none）
//        throw new MessageConversionException("抛出消息格式转换异常！");
        //故意抛出运行时异常；这时候会返回nack；消息不会被删除，会重新入队；再次接收到（配置中消息确认模式为auto）
        throw new RuntimeException("抛出业务异常！");
    }

    //接收dlx.queue队列的消息
    @RabbitListener(queues = "dlx.queue")
    public void listenDlxQueue(String msg) {
        System.out.println(LocalDateTime.now() + "---消费者从dlx.queue队列中接收到消息:" + msg);
    }

    //接收delay.queue队列的消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "delay.queue",durable = "true"),
            //delay = true 表示是延迟交换机；
            exchange = @Exchange(value = "delay.direct",type = ExchangeTypes.DIRECT,durable = "true",delayed = "true"),
            key = "delay"
    ))
    public void listenDelayQueue(String msg){
        System.out.println(LocalTime.now() + "---消费者从 delay.queue 队列中接收到消息："+msg);
    }
}
